package blocks
{
	import org.flixel.FlxG;
	import org.flixel.FlxPoint;
	import org.flixel.FlxTimer;
	import org.flixel.plugin.photonstorm.FlxDelay;

	public class Collapse extends DynamicBlock
	{
		private var speed:Number;
		private var startPos:FlxPoint;
		
		
		/**
		 * this block will collapse after the player walk on it. It will reapear after 15 seconds
		 * 
		 * */
		public function Collapse(X:int, Y:int, infos:Object, mapName:String) {
			super(X, Y, infos, mapName);
			startPos = new FlxPoint(X,Y);
			
			delay = new FlxDelay(infos.timer * 1000);
			speed = infos.speed != 0 ? infos.speed : 1;
			
		}
		
		private var delay:FlxDelay;
		
		override public function activate():void {
			super.activate();
			
			delay = new FlxDelay(infos.timer * 1000);
			delay.callback = falling;
			delay.start();
		}
		
		
		/**
		 * Let the block start to fall and delaying for resetPosition
		 * */
		private function falling():void{
			acceleration.y = speed * Registry.gameInfos.world.gravity;
			delay.reset(15000);
			delay.callback = resetPosition;
		}
		
		
		/**
		 * Put the block back in position
		 * */
		private function resetPosition():void{
			velocity.y = 0;
			x = startPos.x;
			y = startPos.y;
			active = false;
		}
	}
}
